<?php

namespace frontend\modules\marketing\models;

use frontend\modules\orders\models\OrderInfo;
use frontend\modules\orders\models\OrderGoods;
use Yii;
use yii\base\Exception;

/**
 * This is the model class for table "share_reb".
 *
 * @property string $share_reb_id
 * @property string $user_id
 * @property string $buyer_id
 * @property string $reb_money
 * @property integer $created_at
 * @property string $goods_id
 * @property string $order_id
 * @property string $is_payed
 * @property string $goods_number
 */
class ShareReb extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'share_reb';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['share_reb_id','default','value'=>$this->getUUID()],
            ['created_at','default','value'=>time()],
            [['share_reb_id', 'user_id', 'buyer_id', 'created_at', 'goods_id'], 'required'],
            [['reb_money'], 'number'],
            [['goods_number','created_at'], 'integer'],
            [['share_reb_id', 'user_id', 'buyer_id', 'goods_id', 'order_id'], 'string', 'max' => 40],
        ];
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
//            TimestampBehavior::className(),
            // 名为myBehavior4的行为，也是给出了MyBehavior类的配置数组
            'uuid' => [
                'class' => \common\behavior\uuidBehavior::className(),
                'owner' => $this
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'share_reb_id' => Yii::t('app', 'Share Reb ID'),
            'user_id' => Yii::t('app', 'User ID'),
            'buyer_id' => Yii::t('app', 'Buyer ID'),
            'reb_money' => Yii::t('app', 'Reb Money'),
            'created_at' => Yii::t('app', 'Created At'),
            'goods_id' => Yii::t('app', 'Goods ID'),
            'order_id'=> Yii::t('app', 'Goods ID'),
        ];
    }

    /**
     * @inheritdoc
     * @return \frontend\modules\marketing\models\query\ShareRebQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new \frontend\modules\marketing\models\query\ShareRebQuery(get_called_class());
    }

    /**
     * 获取返利后，添加返利记录
     *
     * @param $order_id
     * @return bool
     * @throws Exception
     */
    public function addShareReb($order_id)
    {
        $orderGoods = OrderGoods::findAll(['order_id'=>$order_id]);
        $modelARR = array();
        foreach($orderGoods as $k=>$v) {
            $model = new ShareReb();
            $model->user_id = $v->user_id  ;
            $model->buyer_id = $v->refere_id  ;
            $model->goods_id = $v->goods_id  ;
            $model->created_at = time();
            $model->reb_money = $v->goods_price;
            $model->order_id = $v->order_id;
            if($model->save()){
                continue;
            }
            throw  new Exception(json_encode($model->errors));
        }
        return true;
    }

    /**
     * 购买成功，分享获取商品返利权限
     *
     * @param $orderId
     */
    public function addShareGoods($orderId)
    {
        $orderGoods = OrderGoods::findAll($orderId);
        foreach($orderGoods as $k=>$v) {
            $v->share_times = $v->goods_number*10;
        }
        $orderInfo = OrderInfo::findOne($orderId);
        $orderInfo->is_share = 1;
        return ;
    }
}
